package test.utils;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : example
 * @ Author: XuLeHuang
 * @ Date: 2022/8/27 21:57
 * @ Description:
 */
public class XorEncryptUtil {
    private static void xor(InputStream in, OutputStream out) throws IOException {
        int ch;
        while ((ch = in.read()) != -1){
            ch  = ch^ 0xff;
            out.write(ch);
        }
    }


    public static void encrypt(File src, File des) throws IOException {
        FileInputStream in = new FileInputStream(src);
        FileOutputStream out = new FileOutputStream(des);
        xor(in,out);
        in.close();
        out.close();
    }


    public static byte[] decrypt(File src) throws IOException {
        FileInputStream in = new FileInputStream(src);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        xor(in,out);

        byte[] data = out.toByteArray();
        in.close();
        out.close();
        return data;
    }

    public static void decrypt(File src, File des) throws IOException {
        FileInputStream in = new FileInputStream(src);
        FileOutputStream out = new FileOutputStream(des);
        xor(in,out);
        in.close();
        out.close();
    }


    public static void main(String[] args) throws IOException {
        File src = new File("D:\\development\\environment\\idea\\IdeaProjects\\example\\api-use\\target\\classes\\test\\pojo\\UserConfig.class");
        File des = new File("D:\\UserConfig.class");
        XorEncryptUtil.encrypt(src,des);

        /*byte[] decrypt = XorEncryptUtil.decrypt(des);
        String s = new String(decrypt);
        System.out.println("s = " + s);*/

         /*File file = new File("D:\\development\\environment\\idea\\IdeaProjects\\example\\api-use\\target\\classes\\test\\pojo\\UserConfig.class");
        XorEncryptUtil.decrypt(des , file);*/


    }
}


